package com.maoye.ossp.sys.site.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.site.entity.SiteNav;
import com.maoye.ossp.sys.site.entity.SiteNavMenu;
import com.maoye.ossp.sys.site.service.ISiteNavMenuService;
import com.maoye.ossp.sys.site.service.ISiteNavService;
import com.maoye.ossp.sys.site.utils.BeanToMap;
import com.maoye.ossp.sys.site.utils.MenuUtils;

/**
 * 站点导航菜单Contorller
 * @author lijun
 *
 */
@Controller
@RequestMapping(value = "sys/sitenavmenu/")
public class SiteNavMenuContorller extends BaseController {
	

	/**
	 * 是否显示 (1:是,其它:不是)
	 */
	private static final int IS_SHOW_YES = 1;

	@Resource
	private ISiteNavMenuService siteNavMenuService;
	
	@Resource
	private ISiteNavService siteNavService;

	@RequestMapping(value = "list")
	public String list(SiteNavMenu siteNavMenu,HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<SiteNavMenu> list= siteNavMenuService.getList(BeanToMap.beanToMap(siteNavMenu));
		model.addAttribute("list", list);
		model.addAttribute("siteNavMenu", siteNavMenu);
		return "/sys/site/sitenavmenu/list";
	}
	
	/**
	 * 获取树数据
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "getNode")
	public void getzNodes(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String siteNavId =request.getParameter("siteNavId");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		SiteNavMenu siteNavMenu = new SiteNavMenu();
		siteNavMenu.setSiteNavId(Long.parseLong(siteNavId));
		String json = siteNavMenuService.getzNodes(BeanToMap
				.beanToMap(siteNavMenu));
		out.write(json);
		out.flush();
		out.close();
	}
	
	@RequestMapping(value = "view")
	public String view(HttpServletRequest request,Model model) {
		long siteNavMenuId = Long.parseLong(request.getParameter("siteNavMenuId"));
		SiteNavMenu siteNavMenu = siteNavMenuService.get(siteNavMenuId);
		SiteNavMenu siteNavMenup = siteNavMenuService.get(siteNavMenu.getParentId());
		if(siteNavMenup != null){
			siteNavMenu.setParentName(siteNavMenup.getName());
		}
		SiteNav siteNav = siteNavService.get(siteNavMenu.getSiteNavId());
		if(siteNav != null){
			siteNavMenu.setSiteNavName(siteNav.getName());
		}
		model.addAttribute("siteNavMenu", siteNavMenu);
		return "/sys/site/sitenavmenu/view";
	}
	
	@RequestMapping(value = "edit")
	public String edit(HttpServletRequest request,Model model) {
		long siteNavMenuId = Long.parseLong(request.getParameter("siteNavMenuId"));
		SiteNavMenu siteNavMenu = siteNavMenuService.get(siteNavMenuId);
		SiteNavMenu SiteNavMenup = siteNavMenuService.get(siteNavMenu.getParentId());
		if(SiteNavMenup != null){
			siteNavMenu.setParentName(SiteNavMenup.getName());
		}
		model.addAttribute("siteNavMenu", siteNavMenu);
		return "/sys/site/sitenavmenu/add";
	}
	
	@RequestMapping(value = "add")
	public String add(SiteNavMenu siteNavMenu,Model model) {
		siteNavMenu.setId(0L);
		siteNavMenu.setIsshow(IS_SHOW_YES);
		model.addAttribute("siteNavMenu", siteNavMenu);
		return "/sys/site/sitenavmenu/add";
	}
	
	@RequestMapping(value = "save")
	public String save(SiteNavMenu siteNavMenu, Model model,
			RedirectAttributes redirectAttributes) {
		if (siteNavMenu.getId() == 0) {
			siteNavMenu.setId(idGenerator.getUId());
			siteNavMenuService.create(siteNavMenu);
		} else {
			siteNavMenuService.update(siteNavMenu);
		}
		addMessage(redirectAttributes, "保存数据成功");
		return "redirect:/sys/sitenav/view?siteNavId="+siteNavMenu.getSiteNavId();
	}
	
	@RequestMapping(value = "delete")
	public String delete(HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) {
		long siteNavId = Long.parseLong(request.getParameter("siteNavId"));
		long siteNavMenuId = Long.parseLong(request.getParameter("siteNavMenuId"));
		siteNavMenuService.remove(siteNavMenuId);
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/sys/sitenav/view?siteNavId="+siteNavId;
	}
	
	@RequestMapping(value = "showSysMenu")
	public String showSysMenu(HttpServletRequest request,Model model) {
		String navId = request.getParameter("navId");
		List<SiteNavMenu> menuList = MenuUtils.getMenuListByNavId(navId);
		model.addAttribute("menuList", menuList);
		return "/sys/site/showSysMenu";
	}
	
}
